<?php

  /**
   * This class provides method to interract with integer types.<br />
   * To use mathematical methods, use Math abstract
   * class.<br />
   *
   * @author Dany Mottier
   * @version 1.2
   */
  class Integer extends Base implements IInteger {

    private $integer;

    public function __construct($integer) {
      if (is_int($integer) || intval($integer) != 0) {
        if ($integer >= self::MIN_VALUE && $integer < self::MAX_VALUE) {
          $this->integer = $integer;
        } else {
          throw new ArithmeticException("\$integer value is out of range.");
        }
      } else {
        throw new IllegalArgumentException("\$integer parameter must be an integer.");
      }
    }

    public function __invoke() {
      return $this->integer;
    }

    public function integerValue() {
      return $this->integer;
    }

    public function increment(IInteger $step = null) {
      if (is_null($step)) {
        $step = new Integer(1);
      }

      $this->integer += $step->integerValue();
    }

    public function toString() {
      return strval($this->integer);
    }

    public function __toString() {
      return strval($this->integer);
    }

    public static function parseInt(IString $string) {
      return new Integer(intval($string->__toString()));
    }

    public static function cast($variable) {
      if (is_string($variable) || is_integer($variable)) {
        try {
          return new Integer(intval($variable));
        } catch (Exception $ex) {
          throw new NumberFormatException("\$variable can not be converted to type integer");
        }
      } else {
        throw new IllegalArgumentException("\$variable can not be converted to type integer");
      }
    }

  }

?>
